package leecode

func lowestCommonAncestorM(root *TreeNode, p *TreeNode, q *TreeNode) *TreeNode {
	if root == p || root == q || root == nil {
		return root
	}
	treeLeftNode := lowestCommonAncestorM(root.Left, p, q)
	treeRightNode := lowestCommonAncestorM(root.Right, p, q)
	if treeLeftNode != nil && treeRightNode != nil {
		return root
	}
	if treeRightNode == nil {
		return treeLeftNode
	}
	if treeLeftNode == nil {
		return treeRightNode
	}
	return nil
}
